package com.lxmes.hr.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcAttendBase;
import com.lxmes.hr.domain.NcOrgManage;
import com.lxmes.hr.mapper.NcAttendBaseMapper;
import com.lxmes.hr.mapper.NcOrgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcAttendRgMapper;
import com.lxmes.hr.domain.NcAttendRg;
import com.lxmes.hr.service.INcAttendRgService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;

/**
 * 人工考勤Service业务层处理
 *
 * @author zhangzhiyu
 * @date 2023-02-13
 */
@Service
public class NcAttendRgServiceImpl implements INcAttendRgService
{
    @Autowired
    private NcAttendRgMapper ncAttendRgMapper;

    @Autowired
    private NcOrgManageMapper ncOrgManageMapper;
    @Autowired
    private NcAttendBaseMapper ncAttendBaseMapper;



    @Autowired
    protected Validator validator;

    /**
     * 查询人工考勤
     *
     * @param id 人工考勤主键
     * @return 人工考勤
     */
    @Override
    public NcAttendRg selectNcAttendRgById(Long id)
    {
        return ncAttendRgMapper.selectNcAttendRgById(id);
    }

    /**
     * 查询人工考勤列表
     *
     * @param ncAttendRg 人工考勤
     * @return 人工考勤
     */
    @Override
    public List<NcAttendRg> selectNcAttendRgList(NcAttendRg ncAttendRg)
    {
        List<NcAttendRg> ncAttendRgs = ncAttendRgMapper.selectNcAttendRgList(ncAttendRg);
        return ncAttendRgs;
    }




    /**
     * 新增人工考勤
     *
     * @param ncAttendRg 人工考勤
     * @return 结果
     */
    @Override
    public int insertNcAttendRg(NcAttendRg ncAttendRg)
    {
        String kqDate = ncAttendRg.getKqDate().toString().replace("-","");
        String jobNumber = ncAttendRg.getJobNumber();
        String identyCode = kqDate+jobNumber;
        NcAttendRg queryParam = new NcAttendRg();
        queryParam.setIdentificationCode(identyCode);
        List<NcAttendRg> ncAttendRgs = ncAttendRgMapper.selectNcAttendRgList(queryParam);
        if(ncAttendRgs.size()>0){
            throw new RuntimeException("该名员工在【"+kqDate+"】月已存在考勤记录！");
        }


        NcAttendBase ncAttendBase = new NcAttendBase();
        ncAttendBase.setYearN(kqDate.replace("-",""));
        List<NcAttendBase> ncAttendBases = ncAttendBaseMapper.selectNcAttendBaseList(ncAttendBase);

        for(int i=0;i<2;i++){
            List<NcOrgManage> ncOrgManages = ncOrgManageMapper.selectNcOrgManageList(null);
            if(!StringUtils.isNull(ncOrgManages)){
                ncAttendRg.setGcCode(ncOrgManages.get(0).getJtdm());
                ncAttendRg.setGcName(ncOrgManages.get(0).getJtmc());
            }
            ncAttendRg.setCreateBy(SecurityUtils.getUsername());
            ncAttendRg.setCreateTime(DateUtils.getNowDate());
            ncAttendRg.setIdentificationCode(identyCode);
            ncAttendRg.setYcqDay(new BigDecimal(ncAttendBases.get(0).getYcqts()));
            ncAttendRgMapper.insertNcAttendRg(ncAttendRg);
        }
        return 1;
    }

    /**
     * 修改人工考勤
     *
     * @param ncAttendRg 人工考勤
     * @return 结果
     */
    @Override
    public int updateNcAttendRg(NcAttendRg ncAttendRg)
    {
        NcAttendRg queryParam = new NcAttendRg();
        queryParam.setIdentificationCode(ncAttendRg.getIdentificationCode());
        List<NcAttendRg> ncAttendRgs = ncAttendRgMapper.selectNcAttendRgList(queryParam);

        for(NcAttendRg rg : ncAttendRgs){
            String kqDate = ncAttendRg.getKqDate().toString().replace("-","");
            String jobNumber = ncAttendRg.getJobNumber();
            String newIdentyCode = kqDate+jobNumber;
            rg.setIdentificationCode(newIdentyCode);
            rg.setUpdateTime(DateUtils.getNowDate());
            rg.setUpdateBy(SecurityUtils.getUsername());
            rg.setKqDate(ncAttendRg.getKqDate());
            rg.setEmployeeName(ncAttendRg.getEmployeeName());
            rg.setJobNumber(ncAttendRg.getJobNumber());
            rg.setDepartment(ncAttendRg.getDepartment());
            rg.setBranch(ncAttendRg.getBranch());
            rg.setRzDate(ncAttendRg.getRzDate());
            rg.setZb(ncAttendRg.getZb());
            rg.setJobs(ncAttendRg.getJobs());
            rg.setYcqDay(ncAttendRg.getYcqDay());
            ncAttendRgMapper.updateNcAttendRg(rg);
        }
        return 1;
    }

    @Override
    @Transactional
    public int updateNcAttendRgById(NcAttendRg ncAttendRg) {
        //ncAttendRg只更新状态
        NcAttendRg rg = ncAttendRgMapper.selectNcAttendRgById(ncAttendRg.getId());
        ncAttendRgMapper.updateNcAttendRg(ncAttendRg);
        //paramRg更新总数
        NcAttendRg paramRg = new NcAttendRg();
        //1:查询当前的状态
        if("day1".equals(ncAttendRg.getTag())){
            String cu = rg.getDay1();
            String param = ncAttendRg.getDay1();
            dayMethod(rg,paramRg,cu,param);
        }else if("day2".equals(ncAttendRg.getTag())){
            String cu = rg.getDay2();
            String param = ncAttendRg.getDay2();
            dayMethod(rg,paramRg,cu,param);
        }else if("day3".equals(ncAttendRg.getTag())){
            String cu = rg.getDay3();
            String param = ncAttendRg.getDay3();
            dayMethod(rg,paramRg,cu,param);
        }else if("day4".equals(ncAttendRg.getTag())){
            String cu = rg.getDay4();
            String param = ncAttendRg.getDay4();
            dayMethod(rg,paramRg,cu,param);
        }else if("day5".equals(ncAttendRg.getTag())){
            String cu = rg.getDay5();
            String param = ncAttendRg.getDay5();
            dayMethod(rg,paramRg,cu,param);
        }else if("day6".equals(ncAttendRg.getTag())){
            String cu = rg.getDay6();
            String param = ncAttendRg.getDay6();
            dayMethod(rg,paramRg,cu,param);
        }else if("day7".equals(ncAttendRg.getTag())){
            String cu = rg.getDay7();
            String param = ncAttendRg.getDay7();
            dayMethod(rg,paramRg,cu,param);
        }else if("day8".equals(ncAttendRg.getTag())){
            String cu = rg.getDay8();
            String param = ncAttendRg.getDay8();
            dayMethod(rg,paramRg,cu,param);
        }else if("day9".equals(ncAttendRg.getTag())){
            String cu = rg.getDay9();
            String param = ncAttendRg.getDay9();
            dayMethod(rg,paramRg,cu,param);
        }else if("day10".equals(ncAttendRg.getTag())){
            String cu = rg.getDay10();
            String param = ncAttendRg.getDay10();
            dayMethod(rg,paramRg,cu,param);
        }else if("day11".equals(ncAttendRg.getTag())){
            String cu = rg.getDay11();
            String param = ncAttendRg.getDay11();
            dayMethod(rg,paramRg,cu,param);
        }else if("day12".equals(ncAttendRg.getTag())){
            String cu = rg.getDay12();
            String param = ncAttendRg.getDay12();
            dayMethod(rg,paramRg,cu,param);
        }else if("day13".equals(ncAttendRg.getTag())){
            String cu = rg.getDay13();
            String param = ncAttendRg.getDay13();
            dayMethod(rg,paramRg,cu,param);
        }else if("day14".equals(ncAttendRg.getTag())){
            String cu = rg.getDay14();
            String param = ncAttendRg.getDay14();
            dayMethod(rg,paramRg,cu,param);
        }else if("day15".equals(ncAttendRg.getTag())){
            String cu = rg.getDay15();
            String param = ncAttendRg.getDay15();
            dayMethod(rg,paramRg,cu,param);
        }else if("day16".equals(ncAttendRg.getTag())){
            String cu = rg.getDay16();
            String param = ncAttendRg.getDay16();
            dayMethod(rg,paramRg,cu,param);
        }else if("day17".equals(ncAttendRg.getTag())){
            String cu = rg.getDay17();
            String param = ncAttendRg.getDay17();
            dayMethod(rg,paramRg,cu,param);
        }else if("day18".equals(ncAttendRg.getTag())){
            String cu = rg.getDay18();
            String param = ncAttendRg.getDay18();
            dayMethod(rg,paramRg,cu,param);
        }else if("day19".equals(ncAttendRg.getTag())){
            String cu = rg.getDay19();
            String param = ncAttendRg.getDay19();
            dayMethod(rg,paramRg,cu,param);
        }else if("day20".equals(ncAttendRg.getTag())){
            String cu = rg.getDay20();
            String param = ncAttendRg.getDay20();
            dayMethod(rg,paramRg,cu,param);
        }else if("day21".equals(ncAttendRg.getTag())){
            String cu = rg.getDay21();
            String param = ncAttendRg.getDay21();
            dayMethod(rg,paramRg,cu,param);
        }else if("day22".equals(ncAttendRg.getTag())){
            String cu = rg.getDay22();
            String param = ncAttendRg.getDay22();
            dayMethod(rg,paramRg,cu,param);
        }else if("day23".equals(ncAttendRg.getTag())){
            String cu = rg.getDay23();
            String param = ncAttendRg.getDay23();
            dayMethod(rg,paramRg,cu,param);
        }else if("day24".equals(ncAttendRg.getTag())){
            String cu = rg.getDay24();
            String param = ncAttendRg.getDay24();
            dayMethod(rg,paramRg,cu,param);
        }else if("day25".equals(ncAttendRg.getTag())){
            String cu = rg.getDay25();
            String param = ncAttendRg.getDay25();
            dayMethod(rg,paramRg,cu,param);
        }else if("day26".equals(ncAttendRg.getTag())){
            String cu = rg.getDay26();
            String param = ncAttendRg.getDay26();
            dayMethod(rg,paramRg,cu,param);
        }else if("day27".equals(ncAttendRg.getTag())){
            String cu = rg.getDay27();
            String param = ncAttendRg.getDay27();
            dayMethod(rg,paramRg,cu,param);
        }else if("day28".equals(ncAttendRg.getTag())){
            String cu = rg.getDay28();
            String param = ncAttendRg.getDay28();
            dayMethod(rg,paramRg,cu,param);
        }else if("day29".equals(ncAttendRg.getTag())){
            String cu = rg.getDay29();
            String param = ncAttendRg.getDay29();
            dayMethod(rg,paramRg,cu,param);
        }else if("day30".equals(ncAttendRg.getTag())){
            String cu = rg.getDay30();
            String param = ncAttendRg.getDay30();
            dayMethod(rg,paramRg,cu,param);
        }else if("day31".equals(ncAttendRg.getTag())){
            String cu = rg.getDay31();
            String param = ncAttendRg.getDay31();
            dayMethod(rg,paramRg,cu,param);
        }
        paramRg.setIdentificationCode(rg.getIdentificationCode());
        ncAttendRgMapper.updateNcAttendRgByCode(paramRg);
        return 1;
    }

    /**
     * 批量删除人工考勤
     *
     * @param ids 需要删除的人工考勤主键
     * @return 结果
     */
    @Override
    public int deleteNcAttendRgByIds(Long[] ids)
    {
        return ncAttendRgMapper.deleteNcAttendRgByIds(ids);
    }

    /**
     * 删除人工考勤信息
     *
     * @param id 人工考勤主键
     * @return 结果
     */
    @Override
    public int deleteNcAttendRgById(Long id)
    {
        return ncAttendRgMapper.deleteNcAttendRgById(id);
    }
    @Override
    public String imporIteminfo(List<NcAttendRg> ncAttendRgList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncAttendRgList) || ncAttendRgList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcAttendRg ncAttendRg : ncAttendRgList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncAttendRg);
                ncAttendRg.setCreateBy(operName);
                ncAttendRg.setUpdateBy(operName);
                this.insertNcAttendRg(ncAttendRg);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int deleteNcAttendRgByIdentyCode(String[] ids) {
        return ncAttendRgMapper.deleteNcAttendRgByIdentyCode(ids);
    }


    /*
    * @Param  当前数据
    * @Param  传过来的参数
    * @Param  当前day1...31
    * @Param  参数day1...31
    * */
    public void dayMethod(NcAttendRg rg,NcAttendRg ncAttendRg,String currentval,String paramVlaue){
        {

            //2:根据传值判断是修改还是新增（当前值是否是空值）
            if(StringUtils.isEmpty(currentval)){
                //若是新增则改变相应的字段数据 +0.5（两条数据都改根据唯一识别码）
                if("白班".equals(paramVlaue)){
                    if(StringUtils.isNull(rg.getActDays())){
                        ncAttendRg.setActDays(new BigDecimal(0.5));
                    }else{
                        ncAttendRg.setActDays(rg.getActDays().add(new BigDecimal(0.5)));
                    }
                } else if ("休班".equals(paramVlaue)) {
                    if(StringUtils.isNull(rg.getRestDay())){
                        ncAttendRg.setRestDay(new BigDecimal(0.5));
                    }else{
                        ncAttendRg.setRestDay(rg.getRestDay().add(new BigDecimal(0.5)));
                    }
                } else if ("病假".equals(paramVlaue)){
                    if(StringUtils.isNull(rg.getLeaveDay())){
                        ncAttendRg.setLeaveDay(new BigDecimal(0.5));
                    }else{
                        ncAttendRg.setLeaveDay(rg.getLeaveDay().add(new BigDecimal(0.5)));
                    }
                }
            }else{
                //若是修改则先该原值的对应字段 -0.5再修改现在的字段 +0.5（两条数据都改根据唯一识别码）
                if("白班".equals(currentval)){
                    ncAttendRg.setActDays(rg.getActDays().subtract(new BigDecimal(0.5)));
                } else if ("休班".equals(currentval)) {
                    ncAttendRg.setRestDay(rg.getRestDay().subtract(new BigDecimal(0.5)));
                } else if ("病假".equals(currentval)){
                    ncAttendRg.setLeaveDay(rg.getLeaveDay().subtract(new BigDecimal(0.5)));
                }

                if("白班".equals(paramVlaue)){
                    ncAttendRg.setActDays(Optional.ofNullable(rg.getActDays()).orElse(new BigDecimal(0)).add(new BigDecimal(0.5)));
                } else if ("休班".equals(paramVlaue)) {
                    ncAttendRg.setRestDay(Optional.ofNullable(rg.getRestDay()).orElse(new BigDecimal(0)).add(new BigDecimal(0.5)));
                } else if ("病假".equals(paramVlaue)){
                    ncAttendRg.setLeaveDay(Optional.ofNullable(rg.getLeaveDay()).orElse(new BigDecimal(0)).add(new BigDecimal(0.5)));
                }

            }


        }
    }


}
